//
//  ZLEmotion.swift
//  表情包数据
//
//  Created by zhanglongtao on 17/1/6.
//  Copyright © 2017年 hanju001. All rights reserved.
//

import UIKit
import YYModel

class ZLEmotion: NSObject {

    //表情类型false 图片表情 true emij表情
    var type: Bool = false
    //碧青字符串发送给新浪服务器
    var chs: String?
    // 表情图片名称
    var png: String?
    //emoji的十六进制编码
    var code: String? {
        didSet{
            guard let code = code else{
                
                return
            }
            let scanner = Scanner(string: code)
            var result: UInt32 = 0
            scanner.scanHexInt32(&result)
            let c = Character(UnicodeScalar(result)!)
            
            emoji = String(c)
        }
    }
    //表情使用次数
    var times: Int = 0
    //emoji的字符串
    var emoji: String?
    //表情模型所在的目录
    var directory: String?
    
    //图片表情对应的图像
    var image: UIImage? {
        
        //表情类型
        if type {
            
            return nil
        }
        guard let directory = directory, let png = png, let path = Bundle.main.path(forResource: "HMEmoticon.bundle", ofType: nil), let bundle = Bundle(path: path) else {
            
            return nil
        }
        
        return UIImage(named: "\(directory)/\(png)", in: bundle, compatibleWith: nil)
        
    }
    
    /// 将当前的图片生成图片的属性文本
    func imageText(font: UIFont) -> NSAttributedString {
        
        guard let image = image else{
            
            return NSAttributedString(string: "")
        }
        let attachment = ZLEmotionAttachment();
        
        attachment.chs = chs
        attachment.image = image;
        let height = font.lineHeight;
        attachment.bounds = CGRect(x: 0, y: -4, width: height, height: height)
        
        let attrStrM = NSMutableAttributedString(attributedString: NSAttributedString(attachment: attachment))
        attrStrM.addAttributes([NSFontAttributeName: font], range: NSRange(location: 0, length: 1))
        
        return attrStrM
        
    }
    
    override var description: String {
        
        return yy_modelDescription()
    }
    
}

